package com.example.petstore_2.controller;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.example.petstore_2.common.CommonResponse;
import com.example.petstore_2.domain.CartItem;
import com.example.petstore_2.domain.Item;
import com.example.petstore_2.service.CartService;
import com.example.petstore_2.service.ItemService;
import com.example.petstore_2.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private ItemService itemService;

    /**
     * 用户加入商品到购物车
     * @param itemId
     * @param number
     * @param token
     * @return
     * @throws Exception
     */
    @PutMapping("/cartItems/{itemId}/{number}")
    public CommonResponse userAddToCart(@PathVariable("itemId") String itemId, @PathVariable("number") int number,
                                        @RequestHeader("Authorization") String token) throws Exception {
//        System.out.println(itemId);
//        String itemId = requestBody.getString("itemId");
//        int number = requestBody.getIntValue("number");
        DecodedJWT verify = JWTUtil.verify(token);
        String username =verify.getClaim("username").asString();
        return cartService.userAddItemToCart(username, itemId, number);
    }

    /**
     * 更新购物车中某个单独的商品
     * @param itemId
     * @param number
     * @param token
     * @return
     */
    @PostMapping("/cartItems/{itemId}/{number}")
    public CommonResponse userUpdateCart(@PathVariable("itemId") String itemId,@PathVariable("number") int number, @RequestHeader("Authorization") String token){
//        String itemId = requestBody.getString("itemId");
//        int number = requestBody.getIntValue("number");
        String username = JWTUtil.getTokenInfo(token, "username");
        return cartService.userUpdateCart(username, itemId, number);
    }

    /**
     * 删除购物车中商品
     * @param itemId
     * @param token
     * @return
     */
    @DeleteMapping("/cartItems/{itemId}")
    public CommonResponse userDeleteCartItem(@PathVariable("itemId") String itemId,
                                             @RequestHeader("Authorization") String token){
        String username = JWTUtil.getTokenInfo(token, "username");
        return cartService.userDeleteCartItem(username, itemId);
    }

    /**
     * 进入购物车
     * @param token
     * @return
     */
    @GetMapping("/cartItems")
    public CommonResponse userCart(@RequestHeader("Authorization") String token){
        String username = JWTUtil.getTokenInfo(token, "username");
        CommonResponse commonResponse = cartService.getUerCartItemList(username);
        if(commonResponse.isSuccess()){
            HashMap cartItemItemHashMap = new HashMap<>();
            for(CartItem cartItem : (List<CartItem>)commonResponse.getData()) {
                Item item = (Item) itemService.getItemByItemidForCart(cartItem.getItemid()).getData();
                HashMap map1 = new HashMap<>();
                map1.put("cartItem", cartItem);
                map1.put("item", item);
                cartItemItemHashMap.put(cartItem.getItemid(), map1);
            }
            return CommonResponse.creatForSuccess(cartItemItemHashMap);
        }
        return CommonResponse.creatForError();
    }
}
